package infomanage.service;

import infomanage.bean.Customer;

public class CustomerList {
    private Customer[] customers;
    private int total = 0;

    public CustomerList() {
    }

    public CustomerList(int totalCustomer) {
        customers = new Customer[totalCustomer];
    }

    public boolean addCustomer(Customer customer) {
        if (customers.length <= total) {
            System.out.println("列表已满");
            return false;
        }
        customers[total++] = customer;
        return true;
    }

    public boolean replaceCustomer(int index, Customer customer) {
        if (index > total - 1 || index < 0) {
            System.out.println("不存在该用户！");
            return false;
        }
        customers[index] = customer;
        return true;
    }

    public boolean deleteCustomer(int index) {
        if (index > total - 1 || index < 0) {
            System.out.println("不存在该用户！");
            return false;
        }
        customers[index] = customers[total - 1]; //最后一个元素移到被删除的位置
        customers[--total] = null; //最后一个位置被清空，并且total-1
        return true;
    }

    public Customer[] getAllCustomers() {
        Customer[] custs = new Customer[total];
        if (total >= 0) System.arraycopy(customers, 0, custs, 0, total);
        return custs;
    }

    public Customer getCustomer(int index) {
        if (index > total - 1 || index < 0) {
            System.out.println("不存在该用户！");
            return null;
        }
        return customers[index];
    }

    public int getTotal() {
        return total;
    }
}
